package com.itheima.youdianda.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.youdianda.domain.SysUser;
import com.itheima.youdianda.dto.PageDto;
import com.itheima.youdianda.dto.SysUserDto;
import com.itheima.youdianda.service.SysUserService;
import com.itheima.youdianda.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 用户管理控制层
 */
@Slf4j
@RestController
@RequestMapping("/sys_user")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    @GetMapping("/list")
    //用户分页查询
    public R<PageDto> sysUserList(Integer page, Integer limit, String loginName, Integer status, String phone) {
        PageDto<SysUserDto> pageInfo = sysUserService.sysUserList(page, limit, loginName, status, phone);
        return R.success(pageInfo);
    }

    @GetMapping("/del")
    //用户逻辑删除
    public R<String> sysUserDel(String id) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getId,id);
        sysUserService.remove(queryWrapper);
        return R.success("删除成功");
    }

    @PostMapping("/add")
    public R<String> sysUserAdd(@RequestBody SysUserDto sysUserDto) {
        sysUserService.sysUserAdd(sysUserDto);
        return R.success("新增成功");
    }

    @GetMapping("/rowInfo")
    public R<SysUserDto> sysUserRowInfo(String id) {
        SysUserDto sysUserDto = sysUserService.sysUserRowInfo(id);
        return R.success(sysUserDto);
    }


    @PostMapping("/update")
    public R<String> sysUserUpdate(@RequestBody SysUserDto sysUserDto){
        sysUserService.sysUserUpdate(sysUserDto);
        return R.success("修改成功");
    }
}
